package littlewhisker.managers {
	import flash.events.Event;

	import littlewhisker.communication.EventBroadcaster;
	import littlewhisker.globals.constants.eventtypes.LWEventTypes;
	import flash.events.EventDispatcher;

	import littlewhisker.utils.Looper;

	/**
	 * @author Philip
	 */
	public class LoopManager extends EventDispatcher {
		
		private static var looper:Looper;
		
			 private static var instance:LoopManager;
      private static var allowInstantiation:Boolean;
      

		
		public function LoopManager()
		{
			if (!allowInstantiation) {
            	throw new Error("Error: Instantiation failed: Use SingletonDemo.getInstance() instead of new.");
          	}
		}
		/**
		 * getInstance
		 **/
	
		
      public static function getInstance():LoopManager {
         if (instance == null) {
         	if(!looper) looper = new Looper();
            allowInstantiation = true;
            instance = new LoopManager();
            allowInstantiation = false;
          }
         return instance;
       }
		
       
       public function start():void{
       		looper.start();
       		looper.addEventListener(LWEventTypes.ON_LOOP, onLoop);
       }
       
       public function stop():void{
       		looper.stop();
       		looper.removeEventListener(LWEventTypes.ON_LOOP, onLoop);
       }
       
       public function get useFrames():Boolean{
       		return looper.useFrames;
       }
       
       public function set useFrames(useFrames:Boolean):void{
       		looper.useFrames = useFrames;
       }
       
       public function get interval():Number{
       		return looper.interval;
       }
       
       public function set interval(interval:Number):void{
       		looper.interval = interval;
       }
       
       public function settings( config:Object ):void{
       		if(config.useFrames) useFrames = config.useFrames;
       		if(config.interval) interval = config.interval;
       }
       
       private function onLoop( event:Event ):void{
       		getInstance().dispatchEvent(event);
       }
       
	}
}
